namespace com.crownpack.initiatives.Features.SiteLevel
{
    using System;
    using System.Runtime.InteropServices;

    using com.crownpack.Exceptions.SharePointExceptions;
    using com.crownpack.Extensions.SharePointExtensions;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;

    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>
    [Guid( "1d25fd8e-3c74-414d-9869-fe30287d0002" )]
    public class SiteLevelEventReceiver : SPFeatureReceiver
    {
        #region Fields

        static string _Title;

        #endregion Fields

        #region Properties

        public static Guid ID
        {
            get
            {
                return new Guid( "6f966c8b-b80b-469b-8c92-f49504a047a5" ); // from feature manifest
            }
        }

        public static string Title
        {
            get
            {
                if ( string.IsNullOrEmpty( _Title ) )
                    _Title = SPFarm.Local.GetFeatureDefinition( ID ).GetTitle( );
                return _Title;
            }
        }

        #endregion Properties

        #region Methods

        public override void FeatureActivated( SPFeatureReceiverProperties properties )
        {
            try
            {
                if ( !( properties.Feature.Parent is SPSite ) )
                {
                    throw new FeatureActivationFailedNotScopedForSiteException( ID, Title, properties.GetWebUrl( ) );
                }
                using ( InitiativesConfiguration_Definition_SiteLevel o = new InitiativesConfiguration_Definition_SiteLevel( properties ) )
                    o.Create( );

                using ( Initiatives_Definition_SiteLevel o = new Initiatives_Definition_SiteLevel( properties ) )
                    o.Create( );

                using ( CalculatedColumns_Definition_SiteLevel o = new CalculatedColumns_Definition_SiteLevel( properties ) )
                    o.Create( );

                using ( Final_Definition_SiteLevel o = new Final_Definition_SiteLevel( properties ) )
                    o.Create( );
            }
            catch ( Exception e )
            {
                throw new FeatureActivationException( ID, Title, properties.GetWebUrl( ), e );
            }
        }

        public override void FeatureDeactivating( SPFeatureReceiverProperties properties )
        {
            try
            {
                if ( !( properties.Feature.Parent is SPSite ) )
                {
                    throw new FeatureDeactivationFailedNotScopedForSiteException( ID, Title, properties.GetWebUrl( ) );
                }
                using ( InitiativesConfiguration_Definition_SiteLevel o = new InitiativesConfiguration_Definition_SiteLevel( properties ) )
                    o.Delete( );

                using ( Final_Definition_SiteLevel o = new Final_Definition_SiteLevel( properties ) )
                    o.Delete( );

                using ( CalculatedColumns_Definition_SiteLevel o = new CalculatedColumns_Definition_SiteLevel( properties ) )
                    o.Delete( );

                using ( Initiatives_Definition_SiteLevel o = new Initiatives_Definition_SiteLevel( properties ) )
                    o.Delete( );
            }
            catch ( Exception e )
            {
                throw new FeatureDeactivationException( ID, Title, properties.GetWebUrl( ), e );
            }
        }

        #endregion Methods
    }
}